package cc.leap.web;

import cc.leap.helper.LoginHelper;
import cc.leap.model.AjaxResult;
import cc.leap.model.TableDataInfo;
import cc.leap.model.domain.SysDictData;
import cc.leap.service.SysDictDataService;
import cc.leap.service.SysDictTypeService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.java.Log;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Post;
import org.noear.solon.annotation.Put;
import org.noear.solon.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

/**
 * SysDictDataController
 *
 * @author leap
 * @date 2024/8/19
 */
@Controller
@Mapping("/system/dict/data")
public class SysDictDataController {

    @Inject
    SysDictTypeService dictTypeService;

    @Inject
    SysDictDataService dictDataService;

    @SaCheckPermission("system:dict:list")
    @Get
    @Mapping("/list")
    public TableDataInfo<SysDictData> list(SysDictData dictData) {
        return dictDataService.selectDictDataPage(dictData);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @Get
    @Mapping(value = "/type/{dictType}")
    public AjaxResult dictType(String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (ObjectUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return AjaxResult.success(data);
    }

    /**
     * 查询字典数据详细
     */
    @SaCheckPermission("system:dict:query")
    @Get
    @Mapping(value = "/{dictCode}")
    public AjaxResult getInfo(Long dictCode) {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 新增字典类型
     */
    @SaCheckPermission("system:dict:add")
    @Post
    @Mapping
    public AjaxResult add(@Validated SysDictData dict) {
        dict.setCreateBy(LoginHelper.getUsername());
        return AjaxResult.success(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @SaCheckPermission("system:dict:edit")
    @Put
    @Mapping
    public AjaxResult edit(@Validated SysDictData dict) {
        dict.setUpdateBy(LoginHelper.getUsername());
        return AjaxResult.success(dictDataService.updateDictData(dict));
    }
}
